import 'package:flutter/material.dart';
import 'package:xindong/common/xindong_api_path.dart';
import 'package:xindong/common/xindong_sezhi.dart';
import 'package:xindong/common/xindong_define.dart';
import 'package:xindong/common/xindong_all.dart';
import 'package:xindong/common/xindong_methods.dart';
import 'package:xindong/handle/xindong_common_handle.dart';
import 'package:xindong/manager/network/xindong_networking.dart';
import 'package:xindong/manager/xindong_page_singleton.dart';
import 'package:xindong/manager/xindong_user_controller.dart';
import 'package:xindong/models/xindong_word_class.dart';
import 'package:xindong/routes/common/xindong_input_text_multi_line_yemian.dart';
import 'package:xindong/widgets/common/xindong_app_bar.dart';
import 'package:xindong/widgets/common/xindong_app_bar_text_button.dart';
import 'package:xindong/widgets/common/xindong_common_widget.dart';

class XinDongLiaoTianWordsYeMian extends StatefulWidget {
  const XinDongLiaoTianWordsYeMian({super.key});

  @override
  State<XinDongLiaoTianWordsYeMian> createState() => _XinDongLiaoTianWordsYeMianState();
}

class _XinDongLiaoTianWordsYeMianState extends State<XinDongLiaoTianWordsYeMian> {
  Future<void> _onAdd(String text) async {
    if (text.trim().isEmpty) return;
    showLoading();
    final object = await XinDongNetworking.instance.request(
      XinDongApi.chatCommonWordsAdd,
      method: XinDongHttpMethod.post,
      parameters: {"content": text, "channel": 2}, // 1语音 2文字
    );
    if (object.code == XinDongServerStatus.success) {
      await XinDongCommonHandle().chatCommonWords();
      setState(() {});
    }
    hideLoading();
  }

  Future<void> _onDel(XinDongWordClass model) async {
    showLoading();
    final object = await XinDongNetworking.instance.request(
      XinDongApi.chatCommonWordsDel,
      method: XinDongHttpMethod.post,
      parameters: {"id": model.id}, // 1语音 2文字
    );
    if (object.code == XinDongServerStatus.success) {
      await XinDongCommonHandle().chatCommonWords();
      setState(() {});
    }
    hideLoading();
  }

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) async {
      await XinDongCommonHandle().chatCommonWords();
      setState(() {});
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: XinDongAppBar(
        "常用语",
        actions: [
          XinDongAppBarTextButton(
            name: "添加",
            onTap: () {
              XinDongPageSingleton.instance.pushYeMian(XinDongInputTextMultiLineYeMian(
                title: "自定义常用语",
                placeholder: "输入常用语内容",
                value: "",
                doneCallback: _onAdd,
              ));
            },
          )
        ],
      ),
      body: ListView.separated(
        itemCount: XinDongAll.instance.chatCommonWords.length,
        itemBuilder: (BuildContext context, int index) {
          final model = XinDongAll.instance.chatCommonWords[index];
          return Container(
            alignment: Alignment.centerLeft,
            color: DZColors.cellColor,
            padding: const EdgeInsets.only(left: kPaddingValue, right: kPaddingValue),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Expanded(
                  child: Padding(
                    padding: const EdgeInsets.only(top: kPaddingValue, bottom: kPaddingValue),
                    child: Text(
                      model.content,
                      style: const TextStyle(fontSize: 15, color: DZColors.titleColor, height: 1.2),
                    ),
                  ),
                ),
                if ("${model.user_id}" == XinDongUserController.to.loginId)
                  SizedBox(
                    height: 40,
                    child: TextButton(
                      onPressed: () => _onDel(model),
                      style: ButtonStyle(
                        alignment: Alignment.centerRight,
                        padding: MaterialStateProperty.all(EdgeInsets.zero),
                      ),
                      child: const Text(
                        "删除",
                        textAlign: TextAlign.end,
                        style: TextStyle(color: Colors.red),
                      ),
                    ),
                  ),
              ],
            ),
          );
        },
        separatorBuilder: (BuildContext context, int index) {
          return commonDivider(indent: 0, endIndent: 0);
        },
      ),
    );
  }
}
